// pages/mes/issue/issue.js
const app = getApp();
import Toast from '../../../component/toast/toast';
import utils from '../../../utils/util.js';
Page({

    /**
     * 页面的初始数据
     */
    data: {
        recommends: [],
        pageSize: 8,
        pageIndex: -1,
        hasMore: true,
    },
    getMyIssues() {
        let that = this;
        if (!that.data.hasMore) return;
        let pageI = ++that.data.pageIndex;
        let data = {
            pageSize: that.data.pageSize,
            pageIndex: pageI,
            currentUserInfoId: app.userInfo.Id,
            userinfoId: app.userInfo.Id
        };
        console.log(JSON.stringify(data));
        that.setData({
            pageIndex: pageI
        });
        wx.request({
            url: app.globalData.serverAddress.getMyIssues,
            data: data,
            header: {
                'Authorization': "Bearer " + app.globalData.token
            },
            success: function(res) {
                console.log(res.data);
                if (res.statusCode == 200) {
                    if (res.data.CodeStatus == 20000) {
                        let newList = res.data.BodyContent.List;
                        let count = res.data.BodyContent.TotalCount;
                        let flag = (that.data.pageIndex + 1) * that.data.pageSize < count;
                        newList.forEach(function(item, index) {
                            newList[index]._amount = utils.formatAmount(item.Price);
                        });
                        newList = that.data.recommends.concat(newList);
                        that.setData({
                            recommends: newList,
                            hasMore: flag
                        });
                    } else {
                        that.setData({
                            hasMore: false
                        });
                        Toast(res.data.Message);
                    }
                }
            },
            fail: function(res) {
                console.log(JSON.stringify(res));
            }
        })
    },
    removeService(id, _success) {
        wx.showLoading({
            title: '加载中...',
            mask: true
        });
        wx.request({
            url: app.globalData.serverAddress.removeService,
            data: {
                ServiceId: id,
                UserInfoId: app.userInfo.Id
            },
            header: {
                'Authorization': "Bearer " + app.globalData.token
            },
            method: 'POST',
            success: function(res) {
                if (res.statusCode == 200 && res.data.CodeStatus == 20000){
                    _success(res);
                }
            },
            fail: function(res) {
                console.log("fail" + res);
                Toast("下架失败，请检查网络！");
            },
            complete: function(res) {
                wx.hideLoading();
            },
        })
    },
    selectItem(event) {
        let index = event.currentTarget.dataset.index;
        let dataObj = this.data.recommends[index];
        app.globalData.serviceContent = dataObj.ServiceContent;
        let parmas = JSON.stringify(dataObj).replace(/&nbsp;/g, ' ');
        wx.navigateTo({
            url: '/pages/mes/issue-detail/issue-detail?parmas=' + parmas
        })
    },
    upService(e) {
        let index = e.currentTarget.dataset.index;
        console.log(this.data.recommends[index]);
        if (this.data.recommends[index].Status != 1) {
            let id = this.data.recommends[index].Id;
            let price = this.data.recommends[index].Price;
            wx.navigateTo({
                url: '/pages/order-detail/order-detail?id=' + id + "&price=" + price,
            });
        }
    },
    belowService(e) {
        let index = e.currentTarget.dataset.index;
        let that = this;
        wx.showModal({
            title: '下架服务',
            content: '您确定要下架改服务吗？',
            showCancel: true,
            cancelText: '取消',
            cancelColor: '#E71015',
            confirmText: '确定',
            confirmColor: '#222',
            success: function(res) {
                if(res.confirm){
                    let id = that.data.recommends[index].Id;
                    console.log("id："+id);
                    that.removeService(id , function(res){
                        that.data.recommends.splice(index, 1);
                        that.setData({
                            recommends: that.data.recommends
                        });
                    })
                }
            },
            fail: function(res) {},
            complete: function(res) {},
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        this.getMyIssues();
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {
        // this.getMyIssues(true);
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {
        this.getMyIssues();
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function() {

    }
})